From 26cf00490a3971f61c9a2d4fbef6906eb0205c2f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 20 Mar 2015 17:47:34 -0700 Subject: [PATCH] Fix cargo test filtering for binaries This adds a new `--bin` flag to `cargo test` to specifically say that a binary should be tested. Additionally the dependencies are tweaked such that binaries to not depend on themselves being available. --- src/bin/test.rs | 15 ++++++++++----- src/cargo/ops/cargo_compile.rs | 9 +++++---- src/cargo/ops/cargo_rustc/context.rs | 2 +- tests/test_cargo_test.rs | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/bin/test.rs b/src/bin/test.rs index 6c385c277..b6f9a12e9 100644 --- a/src/bin/test.rs +++ b/src/bin/test.rs @@ -9,6 +9,7 @@ struct Options { flag_jobs: Option, flag_manifest_path: Option, flag_test: Option, + flag_bin: Option, flag_no_default_features: bool, flag_no_run: bool, flag_package: Option, @@ -24,7 +25,8 @@ Usage: Options: -h, --help Print this message - --test NAME Name of the test executable to run + --test NAME Name of the integration test to run + --bin NAME Name of the binary to run tests for --no-run Compile, but don't run tests -p SPEC, --package SPEC Package to run tests for -j N, --jobs N The number of jobs to run in parallel @@ -52,10 +54,13 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path)); config.shell().set_verbose(options.flag_verbose); - let mut tests = Vec::new(); + let (mut tests, mut bins) = (Vec::new(), Vec::new()); if let Some(s) = options.flag_test { tests.push(s); } + if let Some(s) = options.flag_bin { + bins.push(s); + } let ops = ops::TestOptions { no_run: options.flag_no_run, @@ -69,12 +74,12 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { exec_engine: None, release: false, mode: ops::CompileMode::Test, - filter: if tests.len() == 0 { + filter: if tests.len() == 0 && bins.len() == 0 { ops::CompileFilter::Everything } else { ops::CompileFilter::Only { - lib: false, bins: &[], examples: &[], benches: &[], - tests: &tests, + lib: false, examples: &[], benches: &[], + tests: &tests, bins: &bins, } } }, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index d95ae0c12..d74fa59b8 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -208,8 +208,9 @@ fn generate_targets<'a>(pkg: &'a Package, -> CargoResult> { let profiles = pkg.manifest().profiles(); let build = if release {&profiles.release} else {&profiles.dev}; + let test = if release {&profiles.bench} else {&profiles.test}; let profile = match mode { - CompileMode::Test => if release {&profiles.bench} else {&profiles.test}, + CompileMode::Test => test, CompileMode::Bench => &profiles.bench, CompileMode::Build => build, }; @@ -267,14 +268,14 @@ fn generate_targets<'a>(pkg: &'a Package, named `{}`", desc, name))), }; + debug!("found {} `{}`", desc, name); targets.push((t, profile)); } Ok(()) }; try!(find(bins, "bin", TargetKind::Bin, profile)); - try!(find(examples, "example", TargetKind::Example, - &profiles.dev)); - try!(find(tests, "test", TargetKind::Test, &profiles.test)); + try!(find(examples, "example", TargetKind::Example, build)); + try!(find(tests, "test", TargetKind::Test, test)); try!(find(benches, "bench", TargetKind::Bench, &profiles.bench)); } Ok(targets) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index a30b814a0..360887787 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -378,7 +378,7 @@ impl<'a, 'b: 'a> Context<'a, 'b> { // If this is a test profile, then we need to ensure that all binaries // are built. - if profile.test { + if profile.test && (target.is_test() || target.is_bench()) { ret.extend(pkg.targets().iter().filter(|t| t.is_bin()) .map(|t| (pkg, t, self.lib_profile(pkg.package_id())))); } diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 4dd56d6bb..8eccc5742 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -898,7 +898,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured running = RUNNING, dir = prj.url()); - assert_that(prj.cargo_process("test").arg("--test").arg("bin2"), + assert_that(prj.cargo_process("test").arg("--bin").arg("bin2"), execs().with_status(0).with_stdout(expected_stdout.as_slice())); }); -- 2.30.2